
#encoding=utf-8

import os
import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')

data_dir = './'

def generateLogs(file_path):
    log_path = os.path.join(data_dir, file_path)

    record_marks = {'test': {}, 'valid': {}}
    with open(log_path, 'r') as f_in:
        for line in f_in.readlines():
            try:
                items = line.strip().split('\t')
                marks = record_marks[items[1].split(' ')[0].split(':')[1][:-1]]
                iter = int(items[1].split(' ')[1].split(':')[1])
                for mark in items[2:]:
                    index, m = mark.split('=')
                    if index not in marks:
                        marks[index] = []
                    marks[index].append((iter, float(m)))
            except:
                pass

    def preProcess(record_marks):
        print (record_marks['test'])
        print (record_marks['valid'])
        for _, tvmarks in record_marks.items():
            for index, marks in tvmarks.items():
                for i in range(len(marks)):
                    idx, mark = marks[i]
                    if idx == 0:
                        tvmarks[index] = marks[i:]
        print (record_marks['test'])
        print (record_marks['valid'])

    preProcess(record_marks)

    return record_marks

def drawGraph(record_marks, indexs=None):
    plt.figure(1)

    for index, marks in record_marks['test'].items():
        if indexs == None or index in indexs:
            x = np.linspace(0, len(marks), len(marks))
            y = [mark for _, mark in marks]
            plt.plot(x, y, label=index)
    plt.legend(loc='best')

    plt.figure(2)
    for index, marks in record_marks['valid'].items():
        if indexs == None or index in indexs:
            x = np.linspace(0, len(marks), len(marks))
            y = [mark for _, mark in marks]
            plt.plot(x, y, label=index)
    plt.legend(loc='best')

    #plt.show()

def drawGraphInOneGraph(record_markss, legends=None, index='f1'):
    plt.figure(1)  # 创建图表1
    plt.title(index)
    for idx in range(len(record_markss)):
        record_marks = record_markss[idx]
        marks = record_marks['test'][index]
        x = np.linspace(0, len(marks), len(marks))
        y = [mark for _, mark in marks]
        plt.plot(x, y, label=legends[idx])
    plt.legend(loc='best')
    # plt.savefig("dssm"+index+".png")
    plt.show()

# record_marks1 = generateLogs('mvlstm_logs_unigram.txt')
# record_marks2 = generateLogs('mvlstm_logs_bigram.txt')
# record_marks3 = generateLogs('mvlstm_logs_tigram.txt')
# drawGraphInOneGraph([record_marks1, record_marks2, record_marks3], legends=['unigram', 'bigram', 'trigram'], index='f@10')
# drawGraphInOneGraph([record_marks1, record_marks2, record_marks3], legends=['unigram', 'bigram', 'trigram'], index='precision@10')
# drawGraphInOneGraph([record_marks1, record_marks2, record_marks3], legends=['unigram', 'bigram', 'trigram'], index='recall@10')

# record_marks1 = generateLogs('cdssm_logs_unigram.txt')
# record_marks2 = generateLogs('cdssm_logs_bigram.txt')
# record_marks3 = generateLogs('cdssm_logs_tigram.txt')
# drawGraphInOneGraph([record_marks1, record_marks2, record_marks3], legends=['unigram', 'bigram', 'trigram'], index='f@10')
# drawGraphInOneGraph([record_marks1, record_marks2, record_marks3], legends=['unigram', 'bigram', 'trigram'], index='precision@10')
# drawGraphInOneGraph([record_marks1, record_marks2, record_marks3], legends=['unigram', 'bigram', 'trigram'], index='recall@10')

record_marks1 = generateLogs('dssm_logs_unigram.txt')
record_marks2 = generateLogs('dssm_logs_bigram.txt')
record_marks3 = generateLogs('dssm_logs_tigram.txt')
drawGraphInOneGraph([record_marks1, record_marks2, record_marks3], legends=['unigram', 'bigram', 'trigram'], index='f@10')
drawGraphInOneGraph([record_marks1, record_marks2, record_marks3], legends=['unigram', 'bigram', 'trigram'], index='precision@10')
drawGraphInOneGraph([record_marks1, record_marks2, record_marks3], legends=['unigram', 'bigram', 'trigram'], index='recall@10')


# record_marks1 = generateLogs('logs_ranking_1hidden_nocuted_lr0.0001.txt')
# drawGraph(record_marks1,indexs=['f@5','precision@10','recall@10'])